#include "asprite.h"



AnimSprite * create_asprite(char *pic1[], ...)
{

	//memory allocation for the animated sprites structure
	AnimSprite *afig = (AnimSprite *) malloc(sizeof(AnimSprite));
	int x, y;
	
	/*
	//create a standard sprite with first pixmap
	afig->sp = (pic1, NORMAL, x, y, VISIBLE);
	
	//count the number of variable arguments
	va_list var_args; //variable arguments
	int args = 1;
	
	va_start(var_args, pic1); //start reading variable args
	
	while (va_arg(var_args, char **) != NULL)
		args++; //next variable argument
		
	va_end(var_args); //finish using variable arguments
	
	
	//allocate vector of pointers to pixmaps
	afig->map = (char **) malloc(args * sizeof(char *));
	
	//save the first pixmap
	afig->map[0] = afig->sp->map;
	//....
	
	
	//restart reading variable arguments
	va_start(var_args, pic1);
	
	for (int i = 1; i < args; i++){
	
		char **tmp = va_arg(var_args, char **); //next va
		int wd = afig->map[i]->width;
		int hg = afig->map[i]->height;
		
		if ((afig->map[i] = read_xpm(tmp, &wd, &hg)) == NULL ||
			wd != afig->sp->height || hg != afig->sp->width){
			
			//unsucess, release allocated memory
			for (int j = 0; j < i; j++)
				free(afig->map[j]);
			
			free(afig->map);
			free(afig->sp);
			free(afig);
			
			va_end(var_args); //finish using variable arguments. Imperative!
			
			return NULL;
			
		}
	}
	
	va_end(var_args); //finish using variable arguments. Imperative!


	//.....
	*/
	return afig;

}




void draw_shrinking_waterlilies(int speed, char *base)
{

	int lin, col, x = BASE_X, y = BASE_Y;
	Sprite *lily_xxl, *start_lily, *goal_lily;
	
	
	//drawing of the start waterlily
	start_lily = create_sprite(waterlily_xxl, 1, BASE_X, 525, 1);
	draw_sprite(start_lily, BASE_X, 525, base);
	
	//drawing of the goal waterlily
	goal_lily = create_sprite(waterlily_xxl, 1, 650, BASE_Y, 1);
	draw_sprite(goal_lily, 650, BASE_Y, base);
	
	
	//-->desenhar de acordo com o timer!!
	

	//drawing of the "matrix", with the xxl waterlilies
	for (lin = 0; lin < LINES; lin++){
		
		for (col = 0; col < COLUMNS; col++){
			
			//creation of the waterlily instance, with a default position,
			//type normal (1), and visible (1).
			lily_xxl = create_sprite(waterlily_xxl, 1, 50, 75, 1);
			
			draw_sprite(lily_xxl, x, y, base);
			
			x += BASE_X * 2; 
		}
		
		x = BASE_X; //reset of the x coordinate
		
		y += BASE_Y * 2;
	}

}




void destroy_asprite(AnimSprite *fig, char *base)
{

	if (fig == NULL) //if the animated sprite doesn't exist, returns
		return;

	erase_sprite(fig->sp, base); //erases sprite from screen
		
	free(fig->map); //releases memory occupied by the pixmap
	free(fig->sp); //releases memory occupied by the first sprite
	free(fig); //releases memory occupied by fig

	fig = NULL;

}

